package com.huawang.mdesign.model.service.controller;

import com.huawang.business.constants.ParamErrorConstants;
import com.huawang.business.vo.ModelResultVo;
import com.huawang.business.vo.RelationshipVo;
import com.huawang.business.vo.SelectElementNode;
import com.huawang.mdesign.model.service.dto.*;
import com.huawang.mdesign.model.service.vo.AssociateTypeVo;
import com.huawang.mdesign.model.service.vo.BaseResponse;
import com.huawang.mdesign.model.service.vo.BatchRelatedElementVo;
import com.huawang.mdesign.model.service.vo.RelatedElementVo;
import com.huawang.mdesign.model.service.inter.RelationshipService;


import com.huawang.mdesign.model.service.vo.selectelementitem.SelectElementItem;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Tag(name = "关系与连线")
@RestController
@RequestMapping("/relationship")
@Validated
public class RelationshipController {

    @Autowired
    private RelationshipService relationshipService;

    @Operation(summary = "根据两端的模型，查询存在的所有关系")
    @PostMapping("/queryByEnds")
    public BaseResponse<List<RelationshipVo>> queryByEnds(@Valid  @RequestBody QueryRelationshipDTO dto) {
        return BaseResponse.success(relationshipService.queryRelationship(dto));
    }

    @Operation(summary = "批量显示画布上所有关系-批量操作")
    @PostMapping("/batchQueryByEnds")
    public BaseResponse<List<RelationshipVo>> batchQueryByEnds(@Valid  @RequestBody QueryRelationshipBatchDTO dto) {
        return BaseResponse.success(relationshipService.batchQueryElementRelationship(dto));
    }

    @Operation(summary = "批量现实元素之间的关系连线-批量操作")
    @PostMapping("/batchQueryElementRelationship")
    public BaseResponse<List<RelationshipVo>> batchQueryElementRelationship(@Valid  @RequestBody QueryRelationshipBatchDTO dto) {
        return BaseResponse.success(relationshipService.batchQueryElementRelationship(dto));
    }

    @Operation(summary = "根据关系的一端，查询另一端可以创建的模型")
    @PostMapping("/queryValidateEnds")
    public BaseResponse<SelectElementNode> queryValidateEnds(@Valid @RequestBody QueryRelationshipEndDTO dto) {
        return BaseResponse.success(relationshipService.queryValidateEnds(dto));
    }

    @Operation(summary = "根据关系的一端，查询另一端可以创建的模型类型")
    @PostMapping("/queryValidateEndTypes")
    public BaseResponse<List<String>> queryValidateEndTypes(@Valid @RequestBody QueryRelationshipEndDTO dto) {
        return BaseResponse.success(relationshipService.queryValidateEndTypes(dto));
    }

    @Operation(summary = "根据关系的一端，查询另一端可以创建的模型类型-批量查询")
    @PostMapping("/batchQueryValidateEndTypes")
    public BaseResponse<List<String>> batchQueryValidateEndTypes(@Valid @RequestBody BatchQueryRelationshipEndDTO dto) {
        return BaseResponse.success(relationshipService.batchQueryValidateEndTypes(dto));
    }

    @Operation(summary = "根据关系一端，查询拥有这一端的所有关系类型")
    @PostMapping("/queryElementRelations")
    public BaseResponse<AssociateTypeVo> queryElementRelations(
            @RequestBody DisplayRelatedElementDTO displayRelatedElementDto) {
        return BaseResponse.success(relationshipService.queryElementRelations(displayRelatedElementDto));
    }

    @Operation(summary = "根据关系一端，查询拥有这一端的所有关系类型-批量实现")
    @PostMapping("/batchQueryElementRelations")
    public BaseResponse<AssociateTypeVo> batchQueryElementRelations(@RequestBody DisplayRelatedElementBatchDTO dto) {
        return BaseResponse.success(relationshipService.batchQueryElementRelations(dto));
    }

    @Operation(summary = "根据关系的一端，方向和范围，查询此关系的另一端的所有元素")
    @PostMapping("/queryRelationshipElements")
    public BaseResponse<List<RelatedElementVo>> queryRelationshipElements(
            @RequestBody DisplayRelatedElementDTO displayRelatedElementDto) {
        return BaseResponse.success(relationshipService.queryAllRelatedElements(displayRelatedElementDto, displayRelatedElementDto.getLevel()));
    }

    @Operation(summary = "根据关系的一端，方向和范围，查询此关系的另一端的所有元素-批量实现")
    @PostMapping("/batchQueryRelationshipElements")
    public BaseResponse<BatchRelatedElementVo> batchQueryRelationshipElements(@RequestBody DisplayRelatedElementBatchDTO dto) {
        return BaseResponse.success(relationshipService.batchQueryRelationshipElements(dto));
    }

    @Operation(summary = "根据关系，查询两端的模型")
    @GetMapping("/queryEnds")
    public BaseResponse<RelationshipVo> queryEnds(String projectId, String instanceId) {
        return BaseResponse.success(relationshipService.queryEnds(projectId, instanceId));
    }

    @Operation(summary = "判断模型之间能否创建泛化关系")
    @GetMapping("/checkCreateGeneral")
    public BaseResponse<Boolean> checkCreateGeneral(String projectId, String sourceId, String targetId) {
        return BaseResponse.success(relationshipService.checkCreateGeneral(projectId, sourceId, targetId));
    }

    @Operation(summary = "查询模型泛化的所有子模型")
    @GetMapping("/findSpecificsCascade")
    public BaseResponse<List<String>> findSpecificsCascade(String projectId, String instanceId) {
        return BaseResponse.success(relationshipService.findSpecificsCascade(projectId, instanceId));
    }

    @Operation(summary = "是否能够变更关系模型方向")
    @GetMapping("/canConvertRelationshipDirection")
    public BaseResponse<Boolean> canConvertRelationshipDirection(
            @NotBlank(message = ParamErrorConstants.NO_PROJECT_ID) String projectId,
            @NotBlank(message = ParamErrorConstants.NO_INSTANCE_ID) String instanceId) {
        return BaseResponse.success(relationshipService.canConvertRelationshipDirection(projectId, instanceId));
    }

    @Operation(summary = "变更关系模型方向")
    @PostMapping("/changeRelationshipDirection")
    public BaseResponse<ModelResultVo> changeRelationshipDirection(
            @Valid @RequestBody ChangeRelationshipDirectionDTO changeRelationshipDirectionDTO) {
        return BaseResponse.success(relationshipService.changeRelationshipDirection(changeRelationshipDirectionDTO));
    }

    @Operation(summary = "变更关系两端模型")
    @PostMapping("/changeRelationshipEnds")
    public BaseResponse<ModelResultVo> changeRelationshipEnds(
            @Valid @RequestBody ChangeRelationshipEndsDTO changeRelationshipEndsDTO) {
        return BaseResponse.success(relationshipService.changeRelationshipEnds(changeRelationshipEndsDTO));
    }

    @Operation(summary = "判断source与target是否与关系对应端type匹配")
    @PostMapping("/validateRelationCreation")
    public BaseResponse<Boolean> validateRelationCreation(@RequestBody ValidateRelationCreationDto validateDto) {
        return BaseResponse.success(relationshipService.validateRelationCreation(validateDto));
    }

    @Operation(summary = "判断源模型是否为目标模型的子类")
    @GetMapping("/checkSourceIsSubClassOfTarget")
    public BaseResponse<Boolean> checkSourceIsSubClassOfTarget(String projectId, String sourceId, String targetId) {
        return BaseResponse.success(relationshipService.checkSourceIsSubClassOfTarget(projectId, sourceId, targetId));
    }

    @Operation(summary = "获取所有需求的关系类型")
    @GetMapping("/getAllRequirementRelations")
    public BaseResponse<List<SelectElementItem>> getAllRequirementRelations(String projectId) {
        return BaseResponse.success(relationshipService.getAllRequirementRelations(projectId));
    }


}
